package view;

import Dao.UserDao;
import Model.Obj.Card;
import Model.Obj.Member;
import Model.Obj.MemberCard;
import Model.OrcUtils.JDBCUtils;

import java.awt.EventQueue;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;

public class CardGUI extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JTable table;
    private JTextField textField_1;
    private Card card = new Card();
    private DefaultTableModel btm;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    CardGUI frame = new CardGUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public CardGUI() {
        setTitle("\u5361\u7BA1\u7406");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 500, 400);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JLabel lblNewLabel = new JLabel("\u5361 \u53F7");

        textField = new JTextField();
        textField.setColumns(10);

        JButton btnNewButton = new JButton("\u67E5\u8BE2\u5361\u72B6\u6001");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btnNewButtonActionPerformed(e);
            }
        });

        JScrollPane scrollPane = new JScrollPane();

        JLabel lblNewLabel_1 = new JLabel("\u5B58 \u6B3E");

        textField_1 = new JTextField();
        textField_1.setColumns(10);

        JButton btnNewButton_1 = new JButton("\u9884\u5B58\u6B3E");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btnNewButton_1ActionPerformed(e);
            }
        });

        JLabel lblNewLabel_2 = new JLabel("");

        JButton btnNewButton_2 = new JButton("\u9000  \u5361");
        btnNewButton_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btnNewButton_2ActionPerformed(e);
            }
        });

        JButton btnNewButton_3 = new JButton("\u4F1A\u5458\u7981\u7528");
        btnNewButton_3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btnNewButton_3ActionPerformed(e);
            }
        });

        JButton btnNewButton_4 = new JButton("\u4F1A\u5458\u89E3\u7981");
        btnNewButton_4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btnNewButton_4ActionPerformed(e);
            }
        });
        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
                gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addContainerGap()
                                                .addComponent(lblNewLabel)
                                                .addGap(18)
                                                .addComponent(textField, GroupLayout.PREFERRED_SIZE, 111, GroupLayout.PREFERRED_SIZE)
                                                .addGap(48)
                                                .addComponent(btnNewButton))
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addGap(74)
                                                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
                                                        .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 297, GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(lblNewLabel_2)
                                                        .addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
                                                                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                                                .addComponent(lblNewLabel_1)
                                                                                .addGap(18)
                                                                                .addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                                                        .addComponent(btnNewButton_3)
                                                                        .addComponent(btnNewButton_4))
                                                                .addPreferredGap(ComponentPlacement.RELATED, 90, Short.MAX_VALUE)
                                                                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                                                        .addComponent(btnNewButton_1)
                                                                        .addComponent(btnNewButton_2))
                                                                .addGap(12)))))
                                .addContainerGap(103, Short.MAX_VALUE))
        );
        gl_contentPane.setVerticalGroup(
                gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel)
                                        .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(btnNewButton))
                                .addGap(18)
                                .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE)
                                .addGap(18)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(btnNewButton_1)
                                        .addComponent(lblNewLabel_1)
                                        .addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(29)
                                .addComponent(lblNewLabel_2)
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addComponent(btnNewButton_3)
                                .addGap(18)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(btnNewButton_4)
                                        .addComponent(btnNewButton_2))
                                .addContainerGap(24, Short.MAX_VALUE))
        );

        table = new JTable();
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e){
                MYMousePressed(e);
            }
        });

        btm = new DefaultTableModel(
                new Object[][] {
                },
                new String[] {
                        "\u5361\u5185\u4F59\u989D", "\u5361\u72B6\u6001", "\u5361\u7C7B\u578B"
                }
        );
        table.setModel(btm);
        scrollPane.setViewportView(table);
        contentPane.setLayout(gl_contentPane);
    }
    //获取table选中行内容
    private void MYMousePressed(MouseEvent e) {
        int row = table.getSelectedRow();
        this.card.setBALANCE((Integer) table.getValueAt(row,0));
        this.card.setCARDSTATUS((String) table.getValueAt(row,1));
        this.card.setCARDTYPE((String) table.getValueAt(row,2));
    }

    //退卡
    private void btnNewButton_2ActionPerformed(ActionEvent e) {
        int count = UserDao.DeleteMemberCard(this.card.getCARDNO());
        if (count >= 0){
            JOptionPane.showMessageDialog(null,"退卡成功");
        } else {
            JOptionPane.showMessageDialog(null,"退卡失败");
        }
    }

    //会员解禁
    private void btnNewButton_4ActionPerformed(ActionEvent e) {
        int count = UserDao.UpdateMemberCardStatus("正常",this.card.getCARDNO());

        if (count >= 0){
            JOptionPane.showMessageDialog(null,"解禁成功");
        } else {
            JOptionPane.showMessageDialog(null,"解禁失败");
        }
    }

    //会员禁用
    private void btnNewButton_3ActionPerformed(ActionEvent e) {
        int count = UserDao.UpdateMemberCardStatus("禁用",this.card.getCARDNO());
        if (count >= 0){
            JOptionPane.showMessageDialog(null,"禁用成功");
        } else {
            JOptionPane.showMessageDialog(null,"禁用失败");
        }
    }

    //预存款
    private void btnNewButton_1ActionPerformed(ActionEvent e) {
        String balance = textField_1.getText();
        if (this.card.getCARDTYPE().equals("折扣卡")){
            JOptionPane.showMessageDialog(null,"折扣卡无法储值");
            return;
        } else if (textField_1.getText().isEmpty()){
            JOptionPane.showMessageDialog(null,"请输入存款金额");
            balance = textField_1.getText();
            return;
        }
        System.out.println(balance);
        int count = UserDao.UpdateMemberCardBalance(balance,this.card.getCARDNO());
        if (count >= 0){
            JOptionPane.showMessageDialog(null,"充值成功");
        } else {
            JOptionPane.showMessageDialog(null,"充值失败");
        }
    }

    //查询卡状态
    private void btnNewButtonActionPerformed(ActionEvent e) {
        if (textField.getText().isEmpty()){
            JOptionPane.showMessageDialog(null,"请输入卡号");
            return;
        }
        String CardID = textField.getText();
        List<MemberCard> list = UserDao.selectMemberCard(CardID);
        if (list.size() != 0){
            for (MemberCard memberCard : list){
                Vector v = new Vector<>();
                this.card.setCARDNO(memberCard.getCARDNO());
                this.card.setCARDTYPE(memberCard.getCARDTYPE());
                this.card.setCARDSTATUS(memberCard.getCARDSTATUS());
                v.add(memberCard.getBALANCE());
                v.add(memberCard.getCARDSTATUS());
                v.add(memberCard.getCARDTYPE());
                btm.addRow(v);
            }
        } else {
            JOptionPane.showMessageDialog(null,"卡号不存在");
        }
    }
}
